package threshold;

import java.util.Arrays;

/**
 * @Author wujiaming
 * @Description TODO
 * @Date 2023-03-05 20:26
 * @Version 1.0
 * 合并两个有序数组
 */
public class MergeArray {

    /**
     * 合并两个有序数组，将数组2中的内容合并到数组1中
     * 思想遍历数组2中的每一个元素，对当前的第i个元素插入到数组一中
     * @param nums1
     * @param m
     * @param nums2
     * @param n
     */
     public void merge(int[] nums1, int m, int[] nums2, int n) {
         int num = 0,low,high,mid;
         for (int i = 0; i <nums2.length ; i++) {
             num = nums2[i];
             low = 0;high = m-1;
             while(low<=high){
                 mid = (low + high)/2;
                 if(nums1[mid]<num){
                     low = mid+1;
                 }else{
                     high = mid -1;
                 }
             }
             for(int j =m-1;j>=low;j--){nums1[j+1]=nums1[j];}
             nums1[low] = num;
             m++;
         }
    }

    public static void main(String[] args) {
        MergeArray object = new MergeArray();
        int[] nums1 = {1,2,3,0,0,0};
         int[] nums2 = {2,5,6};
         object.merge(nums1,3,nums2,3);
        System.out.println(Arrays.toString(nums1));


    }
}
